from comment.modules.debt_info import Debt_info
from flask_restful import Resource
from flask_restful.reqparse import RequestParser
from comment.modules import db
from comment.utils.decrators import login_required
from financial.resource.transaction.serializers import DebtPaginateSerializer

class Debt(Resource):

    method_decorators = [login_required]
    def get(self):
        '''管理元看到的借款列表'''
        rp = RequestParser()
        rp.add_argument('start')  # 开始时间
        rp.add_argument('end')  # 结束时间
        rp.add_argument('curPage')  # 当前页
        rp.add_argument('perPage')  # 每页数量
        rp.add_argument('approve_status')  # 审批状态

        args = rp.parse_args()
        start = args.get('start')
        end = args.get('end')
        cur_page = int(args.get('curPage'))
        per_page = int(args.get('perPage'))

        #过滤查询
        # 过滤查询
        debt_list = Debt_info.query
        #第一个过滤条件  ,状态
        if start and end:
            #根据起始时间和结束时间过滤
            debt_list = debt_list.filter(db.cast(Debt_info.loanStartDate,db.DATE) >= db.cast(start,db.DATE)) \
                                    .filter(db.cast(Debt_info.loanStartDate,db.DATE) <= db.cast(end,db.DATE)) \
                                    .paginate(cur_page,per_page,error_out=False)
        else:
            debt_list = debt_list.paginate(cur_page,per_page,error_out=False)

        #分页数据序列化
        data = DebtPaginateSerializer(debt_list).to_dict()
        return {'msg':'Success !!','data':data}